package com.binance.binancedemo.recycler;

import android.app.Activity;


import com.binance.binancedemo.utils.L;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.reactivex.disposables.Disposable;

/**
 */
public class CellSubscriptions {
    private static Map<String, WeakCompositeSubscription> sSubscriptionMap = new ConcurrentHashMap<>();
    private static List<String> mActivities = new LinkedList<>();

    public static void onCreate(Activity activity) {
        WeakCompositeSubscription compositeSubscription = new WeakCompositeSubscription();
        String str = activity.toString();
        sSubscriptionMap.put(str, compositeSubscription);
        mActivities.add(str);
    }

    public static void onDestroy(Activity activity) {
        String str = activity.toString();
        mActivities.remove(str);
        WeakCompositeSubscription compositeSubscription = sSubscriptionMap.remove(str);
        if (compositeSubscription != null) {
            compositeSubscription.dispose();
        }
    }

    public static void addSubscription(Disposable subscription) {
        if (mActivities.size() == 0) {
            return;
        }
        String str = mActivities.get(mActivities.size() - 1);
        WeakCompositeSubscription compositeSubscription = sSubscriptionMap.get(str);
        if (compositeSubscription != null) {
            compositeSubscription.add(subscription);
            if (str != null) {
                L.i("add subscription in activity:" + str);
            }
        }
    }
}
